package config

import (
	"github.com/spf13/viper"
	"strings"
	"tg-bot/cli"
	"tg-bot/common"
)

// 基础配置
func InitConf() {
	common.InitSecretKey()
	cli.FlagConfig()
	setConfigFile()

	encryptStr := viper.GetString("encryptor")
	if len(encryptStr) > 0 {
		all := viper.AllKeys()
		for _, v := range all {
			decrypt := strings.TrimSpace(common.ConfigDetailDecrypt(v, viper.GetString(v), []byte(encryptStr)))
			viper.Set(v, decrypt)
		}
	}

	if viper.GetBool("debug") {
		common.SysLog("Use In Debug Mode")
	}

}

// 设置配置文件路径
func setConfigFile() {
	if !common.IsFileExist(*cli.Config) {
		return
	}
	viper.SetConfigFile(*cli.Config)
	if err := viper.ReadInConfig(); err != nil {
		panic(err)
	}
}
